//页面布局
var pageObj = {type:"02-300",height:[,,30],width:[200],paging:true,size:15,htmlObj:["containerDiv","topContainer"]};var sysTypeGrid,sysUrlGrid;
var layoutContainer = create_layout(pageObj);
var dhxWins,editSysUrlWin;
var oneSysInfo,oneUrlInfo,flag=-1;

$(function(){
	$('.dhtmlxLayoutPolySplitterVer div').css("width","3px");//分割线
	layoutContainer.layout.items[0].showHeader();
	layoutContainer.layout.items[0].setText("<span style='font-weight:bold;color:#206988;'>平台列表</span>");
	var bHeight=layoutContainer.layout.items[1].getHeight() ;
	$('#gridbox').css('height',(bHeight-30-20)+'px');
	initSysTypeGrid();
	initSysUrlGrid();
	loadData();
	//----------------------------------------------------------------!!!样式!!!
	layoutContainer.layout.attachEvent("onPanelResizeFinish",function(){
		sysUrlGrid.setSizes();
		if(layoutContainer.layout.items[0].getWidth()>230){
			$func.alert("当前宽度已到达最大限制","red");
			layoutContainer.layout.items[0].setWidth(230);
		}
	});
	//表格列自适应
	layoutContainer.layout.attachEvent("onExpand", function(itemId){
		sysUrlGrid.setSizes();
	});
	layoutContainer.layout.attachEvent("onCollapse", function(itemId){
		sysUrlGrid.setSizes();
	});
	$('#pagingArea').css({"height":"25px","bottom":"5px","left":"208px"});//分页

});
//初始化左侧banner表格
function initSysTypeGrid() {
	sysTypeGrid = layoutContainer.grid[0];
	dmjplugs.grid.createGrid({
		sm:'ch',
		skin:'dhx_skyblue',
		srcc:true,
		validate : false,
		columns:[ {header:'平台编码',id:'sysType',hidden:true},
			{header:'平台名称',id:'sysName',width:'*'}
		]
	},sysTypeGrid);
	sysTypeGrid.setTotal(0);
	sysTypeGrid.enableEditEvents(false,false,false);
	sysTypeGrid.attachEvent("onRowSelect",selectSysType);
	sysTypeGrid.setColumnHidden(0,true);
	loadSysTypeData();
}
//初始化URL白名单信息Grid
function initSysUrlGrid(){
	sysUrlGrid= new dhtmlXGridObject('gridbox');
	enablePaging(pageObj, sysUrlGrid, document.body);
	var columnsInfo=[{header:'id',id:'id',hidden:true},
		{header:'应用ID',id:'appId',width:130},
		{header:'平台类型',id:'sysType',hidden:true},
		{header:'URL',id:'url',width:'*'},
		{header:'备注',id:'remark',width:200},
		{header:'操作时间',id:'gmtModify',width:130},
		{header:'操作人',id:'operator',width:150}
	];
	dmjplugs.grid.createGrid({
		sm:'ch',
		skin:'dhx_skyblue',
		srcc:true,
		columns:columnsInfo
	},sysUrlGrid);
	sysUrlGrid.setTotal(0);
	sysUrlGrid.enableEditEvents(false,false,false);
	sysUrlGrid.setColumnMinWidth(100,4);//设置*列最小宽度 免得缩没了
	sysUrlGrid.attachEvent("loadPagingData", function(first, limit){
		loadData(first, limit);
	});
	sysUrlGrid.attachEvent("onRowDblClicked", function(){
		doEdit('edit');
	});
	sysUrlGrid.setColumnHidden(0,true);
}

//初始化头部按钮控件
function doOnButtFormInit() {
	if (typeof (butt_Form) == 'object') {
		butt_Form.attachEvent("onButtonClick",function(name,command) {
			if (command == 'edit') //编辑
				doEdit(command);
			else if (command == 'add')//新增
				doEdit(command);
			else if (command == 'del') //删除
				doDel();
			else if (command == 'refresh')//刷新
				doUrlRefresh();
		});
	}
}
//选中一家SysType
function selectSysType(rowID, celInd) {
	sysTypeGrid.cells(rowID, sysTypeGrid.getColIndexById('select')).setValue(1);
	loadData();
}
//初始化平台名称
function loadSysTypeData() {
	sysTypeGrid.clearAll();
	sysTypeGrid.setTotal(0);
	//初始化数据
	$.each(sysType,function(index,item){
		//index是索引值（即下标）   item是每次遍历得到的值；
		if(item[0]&&item[0]!=''){
			sysTypeGrid.addRow(index,[0,item[0],item[1]]);
		}
	});

}
//加载URL白名单信息数据
function loadData(first,limit){
	if (!limit) {
		sysUrlGrid.setTotal(0);
		sysUrlGrid.clearAll();
		first = 0;
		limit = sysUrlGrid.getPageRowLimit();
	}
	//查询条件
	var postData="pageNo="+parseInt(first/limit+1)+"&pageSize="+limit+"&pageCount="+sysUrlGrid.getPageTotal();
	//当前选择的svActive
	var sysTypes =  getCheckedRowsByGrid(sysTypeGrid, sysTypeGrid.getColIndexById('select'));
	if(sysTypes&&sysTypes.length>0)
		postData+='&sysType='+sysTypes[0].sysType;
	
	layoutContainer.layout.progressOn("加载智慧URL白名单中...");
	$.ajax({
		url: basePath+'whitelist/findSysUrlGrid',
		type: 'post',
		dataType: 'json',
		data:encodeURI(postData),
		success: function (data) {
			if(!data){
				$func.alert('未找到URL白名单','red');
				return false;
			}
			if(!data.result){
				$func.alert(data.message?data.message:'未找到URL白名单','red');
				return false;
			}
			if(data.rowCount>0) {
				sysUrlGrid.parse(data.gridr, "json");
				sysUrlGrid.setTotal(data.rowCount);
				sysUrlGrid.uncheckAll();
				sysUrlGrid.forEachRow(function(id){
					var gmtModify = $.trim(sysUrlGrid.cells(id, sysUrlGrid.getColIndexById('gmtModify')).getValue());
					if(gmtModify&&gmtModify.length==21){
						var time=gmtModify.substr(0,gmtModify.length-5);
						sysUrlGrid.cells(id, sysUrlGrid.getColIndexById('gmtModify')).setValue(time);
					}
					var url = $.trim(sysUrlGrid.cells(id, sysUrlGrid.getColIndexById('url')).getValue());
					sysUrlGrid.cells(id, sysUrlGrid.getColIndexById('url')).setValue(escapeSign(url));
				});
			}
			var bHeight=layoutContainer.layout.items[1].getHeight() ;
			$('#gridbox').css('height',(bHeight-30-20)+'px');
			layoutContainer.layout.progressOff();
		},
		error:function (XMLHttpRequest, textStatus, statustText) {
			$func.alert('未找到智慧URL白名单','red');
			layoutContainer.layout.progressOff();
			return false;
		}
	});
}
function doEdit(type) {
	if (type != 'add' && type != 'edit')
		return;
	//当前选择的平台类型、url
	if (type == 'add') {
		var sysTypes = getCheckedRowsByGrid(sysTypeGrid, sysTypeGrid.getColIndexById('select'));
		if (!sysTypes || sysTypes.length > 1){
			$func.alert("请选择平台再新增", "red");
			return;
		}
		flag=Math.floor(Math.random()*1000);
		checkParam(flag);//TODO 水平校验
		oneSysInfo=sysTypes[0];
	}else{
		var urls = getCheckedRowsByGrid(sysUrlGrid, sysUrlGrid.getColIndexById('select'));
		if (!urls || urls.length > 1){
			$func.alert("请选择一行再编辑", "red");
			return;
		}
		oneUrlInfo=urls[0];
		checkParam(urls[0].id);
	}
	if (!dhxWins)dhxWins = new dhtmlXWindows();
	dhxWins.setImagePath(basePP + "js/dhtmlx/dhtmlxWindows/codebase/imgs/");
	editSysUrlWin = dhxWins.createWindow("editSysUrlWin", 2, 2, 580,230);
	editSysUrlWin.center();
	editSysUrlWin.setModal(true);
	editSysUrlWin.bringToTop();
	editSysUrlWin.setText("<span style=\"font-size:14px;\">" + (type == "add" ? "新增" : "编辑") + "</span>");
	editSysUrlWin.denyResize();
	editSysUrlWin.button("minmax1").hide();
	editSysUrlWin.button("park").hide();
	editSysUrlWin.attachURL(basePath + "whitelist/openEditSysUrlWin?type=" + type);
}
//删除操作
function doDel(){
	var urls = getCheckedRowsByGrid(sysUrlGrid, sysUrlGrid.getColIndexById('select'));
	if (urls == null||urls.length > 1) {
		$func.alert("请选择一行再删除", "red");
		return;
	}
	checkParam(urls[0].id);
	var confirmArr = "确定删除该项URL纪录吗？ <br><font color='#ff66;'>删除后不可恢复</font>";
	$func.confirm({
		content : confirmArr,
		ok : function() {
			var json ={'id':urls[0].id};
			$.ajax({
				url: basePath+'whitelist/delSysUrl',
				type: 'post',
				dataType: 'json',
				contentType:"application/json",
				data:JSON.stringify(json),
				success: function (data) {
					if(!data){
						$func.alert('删除失败','red');
						return false;
					}
					if(!data.result){
						$func.alert(data.message?data.message:'删除失败','red');
						return false;
					}
					$func.alert("删除成功", "green");
					doUrlRefresh();//刷新
				},
				error:function (XMLHttpRequest, textStatus, statustText) {
					$func.alert("删除失败", "red");
				}
			});
		}
	});
}
//刷新操作
function doUrlRefresh(){
	loadData();
}

